package com.cnap.controller;

import com.cnap.model.common.CommonResult;
import com.cnap.model.file.MergeReq;
import com.cnap.model.file.PatchRes;
import com.cnap.model.file.UploadPatchReq;
import com.cnap.model.progress.ProgressVO;
import com.cnap.service.FileService;
import com.cnap.service.ProgressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * 文件上传控制类
 *
 * @author xc
 **/
@Api(tags = "文件管理")
@RestController
@RequestMapping("/api/file")
public class FileController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class);

    private final FileService fileService;
    private final ProgressService progressService;

    @Autowired
    public FileController(FileService fileService, ProgressService progressService) {
        this.fileService = fileService;
        this.progressService = progressService;
    }

    /*
     由于前端计算MD5缓慢
     所以暂时去除前端传入MD5
     缩短上传时间
     */

    /**
     * 分片上传
     *
     * @param uploadPatchReq 上传参数
     * @param multipartFile  分片文件
     * @return 结果
     */
    @ApiOperation(value = "上传分片", notes = "上传分片", httpMethod = "POST")
    @PostMapping("/patch/upload")
    public CommonResult<PatchRes> uploadPatch(@Validated UploadPatchReq uploadPatchReq, @RequestParam(
            "patch") MultipartFile multipartFile) {
        LOGGER.info("[file]: upload patch file, {}", uploadPatchReq);
        PatchRes patchRes = fileService.uploadPatch(uploadPatchReq, multipartFile);
        return CommonResult.success(patchRes);
    }

    /*
    分片合并，必须传入父文件i
     */

    /**
     * 合并文件
     *
     * @param mergeReq 合并参数
     * @return 结果
     */
    @PostMapping("/patch/merge")
    @ApiOperation(value = "合并文件", notes = "合并文件", httpMethod = "POST")
    public CommonResult<String> mergePatch(@Validated @RequestBody MergeReq mergeReq) {
        LOGGER.info("[file]: merge patch file, {}", mergeReq);
        fileService.mergePatchFile(mergeReq);
        return CommonResult.success("success");
    }

    /*
    进度条id与父id相同
     */

    /**
     * 查询上传进度
     *
     * @param id 应用id
     * @return 结果
     */
    @GetMapping("/progress/{id}")
    @ApiOperation(value = "查询进度", notes = "查询进度", httpMethod = "GET")
    public CommonResult<ProgressVO> queryProgress(@PathVariable("id") String id) {
        ProgressVO progressVO = progressService.getProgressByApplicationId(id);

        return Objects.nonNull(progressVO) ? CommonResult.success(progressVO) : CommonResult.failed("未找到该应用上传进度");
    }
}
